package com.dgw.springbootrabbitmq.receiver;

import com.dgw.springbootrabbitmq.config.DirectConfig;
import com.dgw.springbootrabbitmq.entity.Goods;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
//@RabbitListener(queues = DirectConfig.DIRECT_QUEUE)    // queues是指要监听的队列的名字
public class DirectReceiver {

    private static final Logger log = LoggerFactory.getLogger(DirectReceiver.class);
    // 处理具体的任务
    @RabbitHandler
    public void receiverDirectQueue(Goods goods, Channel channel, Message message) throws IOException {
        // 采用手动应答模式, 手动确认应答更为安全稳定
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        //手工ack
        channel.basicAck(deliveryTag,true);
        log.info("简单消息接受成功，参数是：" + goods.toString());
    }
}
